﻿<cfsilent>
	<cfscript>
	
		sql = "SELECT sbj_id, sbj_name 
				FROM t_subject  
				WHERE sbj_id = :sbjId " ;
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="sbjId", value=event.getArg('SubID'), cfsqltype="cf_sql_varchar" );
		rs_subjectEntity = queryObj.execute( sql=sql ).getResult();

		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");

		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		grade = event.getArg("Grade");

		event.setArg("pageTitle", "教学运行基础数据 - 教学计划 - 专业学期开课计划 - " & rs_subjectEntity.sbj_name);

		if ( len(academicYear) and len(academicTerm) ) {
			
			sql = "SELECT term_id, year_term  
					FROM t_term  
					WHERE academic_year = :academicYear 
						  AND 
						  academic_term = :academicTerm " ;
			queryObj = new query( datasource=application.dnsSlave );
			queryObj.addParam( name="academicYear", value=academicYear, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="academicTerm", value=academicTerm, cfsqltype="cf_sql_char" );
			rs_term = queryObj.execute( sql=sql ).getResult();

			sql = "SELECT sbj_id 
					FROM t_subject  
					WHERE sbj_id LIKE :condition " ;
			queryObj = new query( datasource=application.dnsSlave );
			queryObj.addParam( name="condition", value=event.getArg('SubID') & '_', cfsqltype="cf_sql_varchar" );
			rs_dir = queryObj.execute( sql=sql ).getResult();

			sql = "select grade,countPlan from (SELECT 
									substr(t_class.grade,1,4) grade, COUNT(DISTINCT t_plan.cid) countPlan 
								FROM  
									t_plan 
									INNER JOIN t_class ON t_class.cls_id = t_plan.cls_id 
								WHERE 
									t_class.sbj_id = :subjectId 
									AND 
									t_plan.term_id = :termId 
								GROUP BY 
									grade 
								ORDER BY 
									grade DESC )
									
							group by grade,countPlan 
							ORDER BY GRADE DESC" ;
			queryObj = new query( datasource=application.dnsSlave );
			
			queryObj.addParam( name="subjectId", value=rs_subjectEntity.sbj_id, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=rs_term.term_id, cfsqltype="cf_sql_char" );
			
			rs_grade = queryObj.execute( sql=sql ).getResult();
		
	
			if ( rs_grade.recordCount ) {
				currentGrade = rs_grade.grade;
			}
		
			if ( len(grade) ) {
				currentGrade = grade;
			}
		}

		passArgs = structNew();
		structInsert(passArgs, "TabID", "labDetail", true);

		targetArgs = structNew();

	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('planSchema', passArgs)#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">教学计划</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 info"></i><cfoutput>#rs_subjectEntity.sbj_name#</cfoutput> 专业学期开课计划
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<cfif len(academicYear) and len(academicTerm)>
			<!--- 活动学期数据正确 --->
			
			<li>
				
				<div class="UItab">
					<div class="tabLables">
                        <cfloop query="rs_grade">
							<span <cfif currentGrade eq rs_grade.grade>class="active"</cfif> id="labCourse<cfoutput>#rs_grade.grade#</cfoutput>" tabTarget="Course<cfoutput>#rs_grade.grade#</cfoutput>"><cfoutput>#rs_grade.grade#</cfoutput>级</span>
						</cfloop>
						<span <cfif rs_grade.recordCount eq 0>class="active"</cfif> id="labRebuilt" tabTarget="Rebuilt">下达专业教学任务</span>
					</div>
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
						
							/* init Tab */
							$("div.tabContent[@id!=' + $targetID + ']").hide();
							$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
							$("div.tabLables > span").click(function(){
								var $targetID = $(this).attr("tabTarget");
							
								$("div.tabLables span").removeClass("active");
								$(this).addClass("active");
							
								$("div.tabContent[@id!=' + $targetID + ']").hide();
								$("div.tabContent#" + $targetID).show();
							});
						
						});
						// -->
						//]]>
					</script>
				</div>
				
				<div id="Rebuilt" class="tabContent">
					
					<div class="systemNotice">
						<p>注意: 该操作会根据培养方案的专业课程设置, 重新生成该专业所有年级行政班的开课计划.</p>
						<p><a id="rebuiltTrigger" href="javascript:void(0);">重新构建该专业所有班级开课计划</a></p>
					</div>
					
					<cfset sbjList = "'" & rs_subjectEntity.sbj_id & "'" />
                    <cfloop query="rs_dir">
						<cfset sbjList = listAppend(sbjList, "'" & rs_dir.sbj_id & "'") />
					</cfloop>
							
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("#rebuiltTrigger").click(function(){
								
								var subject = [<cfoutput>#sbjList#</cfoutput>];
								
								/* built each subject task */
							
								function progress(current) {
								
									$.post(
										"<cfoutput>#buildURL('apiRebuildSubjectPlan')#</cfoutput>",
										{
											'subjectID': subject[current],
											'academicYear': '<cfoutput>#academicYear#</cfoutput>',
											'academicTerm': '<cfoutput>#academicTerm#</cfoutput>',
											'seed': (new Date()).getTime()
										},
										function(date){
										
											response = $.parseJSON(date);
											
											if (response.success) {
												current++;
												if ( current < subject.length) {
													progress(current);
												}
												else {
													window.location.reload(true);
												}
											}
											else {
												alert("班级教学任务重建出错");
											}
											
										});
								}
							
								progress(0);
								
							
							});
							
						});
						// -->
						//]]>
					</script>
				</div>
				
                <cfloop query="rs_grade">
				
                    <cfset sql = "SELECT
										t_course.cid,
										t_course.course_name, 
										t_course_nature.cna_name, 
										t_course_prop.prop_name, 
										t_course.course_credit, 
										t_institute.institute_short, 
										t_plan.sbj_direction 
									FROM 
										t_plan 
										INNER JOIN t_course ON t_course.cid = t_plan.cid 
										INNER JOIN t_course_nature ON t_course_nature.cna_id = t_plan.cna_id 
										INNER JOIN t_course_prop ON t_course_prop.prop_id = t_plan.prop_id 
										INNER JOIN t_institute ON t_institute.institute_id = t_course.institute_id 
										INNER JOIN t_class ON t_class.cls_id = t_plan.cls_id 
									WHERE 
										t_class.sbj_id = :subjectId 
										AND 
										t_plan.term_id = :termId 
										AND 
										substr(t_class.grade,1,4) = :grade 
									GROUP BY 
										t_course.cid, 
										t_course.course_name, 
										t_course_prop.prop_name, 
										t_course_nature.cna_name, 
										t_course.course_credit, 
										t_institute.institute_short, 
										t_plan.sbj_direction 
									ORDER BY 
										t_plan.sbj_direction, t_course_prop.prop_name, t_course.course_name " />
					<cfset queryObj = new query( datasource=application.dnsSlave ) />
					<cfset queryObj.addParam( name="subjectId", value=rs_subjectEntity.sbj_id, cfsqltype="cf_sql_varchar" ) />
					<cfset queryObj.addParam( name="termId", value=rs_term.term_id, cfsqltype="cf_sql_char" ) />
					<cfset queryObj.addParam( name="grade", value=left(rs_grade.grade,4), cfsqltype="cf_sql_char" ) />
					<cfset rs_course = queryObj.execute( sql=sql ).getResult() />
                     
					<div id="Course<cfoutput>#rs_grade.grade#</cfoutput>" class="tabContent">
						
						<div class="noticeBlock">
							<p><span class="img icon16x16 message"></span><cfoutput>#rs_grade.grade#</cfoutput>级计划设课 <cfoutput>#rs_grade.countPlan#</cfoutput> 门</p>
						</div>
						
						<cfset row = 0 />
						<table class="UIEditable">
							<thead>
								<tr>
									<td width="18"></td>
									<td width="168">类别</td>
									<td width="48">课程性质</td>
									<td>课程</td>
									<td width="24" align="right">学分</td>
									<td width="48">开课学院</td>
									<td>方向课</td>
									<td width="18"></td>
								</tr>
							</thead>
							<tbody>
                                <cfloop query="rs_course">
									
									<cfset structInsert(targetArgs, "CID", rs_course.cid, true) />
									<cfset structInsert(targetArgs, "TID", rs_term.term_id, true) />
									<cfset structInsert(targetArgs, "SubID", rs_subjectEntity.sbj_id, true) />

									<cfset row ++ />
									<tr class="editRows" rowid="<cfoutput>#rs_course.cid#</cfoutput>">
										<td><span class="index"><cfoutput>#row#</cfoutput></span></td>
										<td><cfoutput>#rs_course.cna_name#</cfoutput></td>
										<td><cfoutput>#rs_course.prop_name#</cfoutput></td>
										<td><cfoutput>#rs_course.course_name#</cfoutput></td>
										<td><span class="numeric"><cfoutput>#numberFormat(rs_course.course_credit,'_._')#</cfoutput></span></td>
										<td><cfoutput>#rs_course.institute_short#</cfoutput></td>
										<cfif rs_course.sbj_direction neq "0">
											<cfset dirIds = [] />
											<cfloop array="#listToArray(rs_course.sbj_direction, '/')#" index="dir">
												<cfif rs_dir.sbj_id neq "0">
													<cfset arrayAppend(dirIds, rs_subjectEntity.sbj_id & dir) />
												</cfif>
											</cfloop>
                                         
                                            <cfset sql = "SELECT sbj_short 
															FROM t_subject 
															WHERE sbj_id IN (:dirIds)" />
											<cfset queryObj = new query( datasource=application.dnsSlave ) />
											<cfset queryObj.addParam( name="dirIds", value=ArrayToList(dirIds), cfsqltype="cf_sql_varchar", list=true ) />
											<cfset rs_direction = queryObj.execute( sql=sql ).getResult() />
                                        
											<td><cfoutput>#valueList(rs_direction.sbj_short, " / ")#</cfoutput></td>
											<cfelse>
											<td></td>
										</cfif>
										<td><a href="<cfoutput>#buildURL('planSchemaClassByCourse', targetArgs)#</cfoutput>" class="stat info"></span></td>
									</tr>
								</cfloop>
							</tbody>
						</table>

					</div>
					
				</cfloop>

			</li>
			
			<cfelse>
			<!--- 要求设置一个数据处理学期 --->
			<li>
				<div class="clearfix" id="termSelector">
					<cfset sql = "SELECT
								    term_id, 
								    academic_year, 
								    academic_term 
							    FROM 
								    t_term 
							    WHERE 
								    SUBSTR(academic_year, 1, 4) > :yearStart 
								    AND 
								    SUBSTR(academic_year, 1, 4) < :yearEnd 
							    ORDER BY 
								    term_id " />
                                            
					<cfset queryObj = new query( datasource=application.dnsSlave ) />
                         
					<cfset queryObj.addParam( name="yearStart", value=(year(now())-2), cfsqltype="cf_sql_char" ) />
					<cfset queryObj.addParam( name="yearEnd", value=(year(now())+1), cfsqltype="cf_sql_char" ) />
                         
                   		<cfset rs_term = queryObj.execute( sql=sql ).getResult() />
                                            
					<div class="noticeBlock">
						<p><span class="img icon16x16 message"></span>请指定当前教学计划的学期</p>
						<ol>
                            <cfloop query="rs_term">
								<li class="friendly"><span class="img icon10x10 execute"></span><a rowid="<cfoutput>#rs_term.term_id#</cfoutput>" href="javascript:void(0);"><cfoutput>#rs_term.academic_year#</cfoutput>-<cfoutput>#dictAdvice.getTermName(rs_term.academic_term)#</cfoutput></a></li>
							</cfloop>
						</ol>
					</div>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("a[rowid]").click(function(){
							
								var rowid = $(this).attr("rowid");
							
								$.post(
										"<cfoutput>#buildURL('apiTermPropChange')#</cfoutput>", 
										{
											'tid' : rowid,
											'prop' : "ForCourse",
											'status' : true,
											'seed': (new Date()).getTime()
										}, 
										function(data) {
										
											result = $.parseJSON(data);
										
											if (result.ForCourse) {
												window.location.reload(true);
											}
										
										});
							});
							
						});
						// -->
						//]]>
					</script>
					
				</div>
			</li>
		</cfif>		
	</ul>
</div>

